# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=rst2pdf
pkgver=0.101
pkgrel=1
pkgdesc="Use a text editor. Make a PDF"
arch=(any)
url="https://github.com/rst2pdf/rst2pdf"
license=(MIT)
depends=(
  python
  python-docutils
  python-importlib-metadata
  python-jinja
  python-packaging
  python-pillow
  python-pygments
  python-reportlab
  python-smartypants
  python-yaml
)
makedepends=(
  python-build
  python-installer
  python-setuptools-scm
  python-wheel
)
# TODO: package python-xhtml2pdf
# https://github.com/xhtml2pdf/xhtml2pdf/issues/560
checkdepends=(
  graphviz
  plantuml
  python-aafigure
  python-matplotlib
  python-pytest
  python-pymupdf
  python-sphinx
  python-svglib
)
# TODO: package python-xhtml2pdf
optdepends=(
  'plantuml: for UML diagram support'
  'python-aafigure: for ASCII image support'
  'python-pyphen: for hyphenation support'
  'python-matplotlib: for math support'
  'python-sphinx: for sphinx support'
  'python-svglib: for SVG support'
)
source=($pkgname-$pkgver.tar.gz::$url/archive/refs/tags/$pkgver.tar.gz)
sha512sums=('096a690e9ad7ff5c9044df5e03f619e7aeda46ccc71c069a6f3832707bb94b8828acec6cedac33f0170475abb531281ba5f871e6194e40cdaffb8762875fa9fe')
b2sums=('5d94a846efb73d01b0f39bf2a64fa5bd1e2bc88993495fbc06f56ae3fb10d0a1b7b0066d8a9dd0037fdca2352d2f260537bf5fbd902c1f2251e049e265d825f6')

build() {
  cd $pkgname-$pkgver
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
  python -m build --wheel --no-isolation
}

check() {
  local pytest_options=(
    -vv
    # https://github.com/rst2pdf/rst2pdf/issues/1067
    --deselect tests/input/test_raw_html.rst::test_raw_html
    --deselect tests/input/sphinx-versionmodified/conf.py::sphinx-versionmodified
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgname-$pkgver
  python -m installer --destdir="test_dir" dist/*.whl

  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"
  export PATH="$PWD/test_dir/usr/bin:$PATH"
  pytest "${pytest_options[@]}"
}

package() {
  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm 644 {CHANGES,MAINTAINERS,README}.rst -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 LICENSE.txt -t "$pkgdir/usr/share/licenses/$pkgname/"
}
